package org.example.SolutionNew006;

import java.util.Arrays;

public class BinarySearch003 {
    public static void main(String[] args) {
        BinarySearch003 binarySearch003 = new BinarySearch003();
        int[] nums=new int[]{1};
        System.out.println(binarySearch003.searchRange(nums,1)[0]);
    }

    public int[] searchRange(int[] nums, int target) {
        int left=binarySearch(nums,target,true);
        int right=binarySearch(nums,target,false)-1;
        int[] res=new int[]{-1,-1};
        if(left<=right&&right<nums.length&&nums[left]==target&&nums[right]==target){
            res[0]=left;
            res[1]=right;
        }
        return res;
    }

    public int binarySearch(int[] nums, int target, boolean low) {
        int left=0,right=nums.length-1,ans= nums.length;
        while(left<=right){
            int mid=left+(right-left)/2;
            if(nums[mid]>target || (low && nums[mid]>=target)){
                right=mid-1;
                ans=mid;
            }else{
                left=mid+1;
            }
        }
        return ans;
    }
}
